package com.jt.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class
UserDetailsServiceImpl implements UserDetailsService {
    @Autowired
    private RemoteUserService remoteUserService;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        com.jt.pojo.User user = remoteUserService.doSelectUserByUserName(username);
        if (user==null) throw new UsernameNotFoundException("用户名不存在");
        List<String> permissions = remoteUserService.doSelectUserPermissions(user.getId());
        log.info("permissions {} ",permissions);
        User userInfo = new User(username, user.getPassword(), AuthorityUtils.createAuthorityList(permissions.toArray(new String[]{})));
        return userInfo;
    }
}
